# -*- coding: utf-8 -*-
# @File  : csdn.py
# @Author: ggy
# @Date  : 2019/8/9
# @Software: PyCharm
import traceback
import lxml.etree as le
import os
# 导入请求连接
from csdn_request import *

keyword = input("请输入关键词：")
page_start = int(input("请输入起始页："))
page_end = int(input("请输入结束页："))

if not os.path.exists('./blog'):
    os.mkdir('./blog')

for page in range(page_start, page_end + 1):
    url = "https://so.csdn.net/so/search/s.do?p=%d&q=%s&t=blog&domain=&o=&s=&u=&l=&f=&rbg=0"
    new_url = format(url % (page, keyword))
    try:
        request = getRequest(new_url)
        # 获取当前 url 获得后的 html信息
        res = getProxyOpener().open(request).read()
        # 解析定位到当前的文章的 href 地址
        href_s = le.HTML(res).xpath('//span[@class="down fr"]/../span[@class="link"]/a/@href')

        for href in href_s:
            try:
                response_blog = getProxyOpener().open(
                    getRequest(href)
                ).read()
                # 发送请求到每一个连接，然后获取定位到当前页面的文章标题
                title = le.HTML(response_blog).xpath('//h1[@class="title-article"]/text()')[0]
                blog_name = 'blog/%s.html' % title
                with open(blog_name, 'wb') as f:
                    f.write(response_blog)
                    print(title, "下载完成！")
            except:
                # 将错误写入错误日志
                errorFile = open('blog/log.txt', 'a')
                errorFile.write(traceback.format_exc())
                print("错误已写入！")
                errorFile.close()
    except:
        # 将错误写入错误日志
        errorFile = open('log.txt', 'a')
        errorFile.write(traceback.format_exc())
        print("错误已写入！")
        errorFile.close()
